using System;
using System.ComponentModel;
using System.DirectoryServices;

namespace RollManager.Library.ActionSteps
{
    [Serializable]
    public class RemoveUserStep : ActionStep
    {
        private string m_domain = "MYFAMILY";
        private string m_group = StepConstants.NAME;
        private string m_user = StepConstants.NAME;

        [Description( "The domain of the user to remove from the group." )]
        public string Domain
        {
            get
            {
                return m_domain;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Domain cannot be empty." );
                }
                else
                {
                    m_domain = value.Trim();
                }
            }
        }

        [DisplayName( "User Group" )]
        [Description( "The user group to remove the user user from." )]
        public string Group
        {
            get
            {
                return m_group;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "User Group cannot be empty." );
                }
                else
                {
                    m_group = value.Trim();
                }
            }
        }

        [DisplayName( "Username" )]
        [Description( "The name of the user to remove from the group." )]
        public string User
        {
            get
            {
                return m_user;
            }
            set
            {
                if( string.IsNullOrEmpty( value ) )
                {
                    throw new ArgumentException( "Username cannot be empty." );
                }
                else
                {
                    m_user = value.Trim();
                }
            }
        }

        public override string Name
        {
            get { return "Remove User"; }
        }

        protected override void Run()
        {
            DirectoryEntry theGroup = getUserGroup();

            String userPath = String.Format( "WinNT://{0}/{1},user", m_domain, m_user );
            theGroup.Invoke( "Remove", new object[] { userPath } );
            theGroup.CommitChanges();

            Progress.AddFormat( "User {0}\\{1} removed from {2}.", m_domain, m_user, m_group );
        }

        protected override void Simulate()
        {
            // Check for group on computer
            getUserGroup();

            Progress.AddFormat( "Simulated removing user {0}\\{1} from {2}.", m_domain, m_user, m_group );
        }

        private DirectoryEntry getUserGroup()
        {
            String groupPath = String.Format( "WinNT://{0}/{1},group", CurrentServer, m_group );
            DirectoryEntry theGroup = new DirectoryEntry( groupPath );
            if( theGroup.SchemaClassName != "Group" )
            {
                throw new ArgumentException( "The local group specified doesn't exist." );
            }

            return theGroup;
        }
    }
}
